<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>mongodb笔记(七) 进阶指南 | 净土</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="mongodb advanced topics, mongodb 进阶">
<meta property="og:type" content="article">
<meta property="og:title" content="mongodb笔记(七) 进阶指南">
<meta property="og:url" content="http://howiefh.github.io/2014/06/04/mongodb-note-7-advanced-topics/index.html">
<meta property="og:site_name" content="净土">
<meta property="og:description" content="mongodb advanced topics, mongodb 进阶">
<meta property="og:updated_time" content="2014-12-24T07:55:07.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="mongodb笔记(七) 进阶指南">
<meta name="twitter:description" content="mongodb advanced topics, mongodb 进阶">
  
    <link rel="alternative" href="/atom.xml" title="净土" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.ico">
  
  <link href="http://fonts.useso.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="/css/style.css" type="text/css">
  
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-40492061-1', 'auto');
ga('send', 'pageview');

</script>


  
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?56d2899c5e919fbf4a7b00de5d1c31dd";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>


</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">净土</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">乐不在外而在心，心以为乐，则是境皆乐；心以为苦，则无境不苦。</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-github-link" class="nav-icon" href="https://github.com/howiefh" title="Github" target="_blank"></a>
        
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed" target="_blank"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://howiefh.github.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-mongodb-note-7-advanced-topics" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    
<a href="/2014/06/04/mongodb-note-7-advanced-topics/" class="article-date">
  <time datetime="2014-06-04T01:43:00.000Z" itemprop="datePublished">2014-06-04</time>
</a>


    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Database/">Database</a>►<a class="article-category-link" href="/categories/Database/MongoDB/">MongoDB</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      mongodb笔记(七) 进阶指南
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
		
		<div id="toc" class="toc-article">
			<h2 class="toc-title"><span>Contents</span></h2>
		
			<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#数据库命令"><span class="toc-number">1.</span> <span class="toc-text">数据库命令</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#命令工作原理"><span class="toc-number">1.1.</span> <span class="toc-text">命令工作原理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#命令参考"><span class="toc-number">1.2.</span> <span class="toc-text">命令参考</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#固定集合"><span class="toc-number">2.</span> <span class="toc-text">固定集合</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#属性"><span class="toc-number">2.1.</span> <span class="toc-text">属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建固定集合"><span class="toc-number">2.2.</span> <span class="toc-text">创建固定集合</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#自然排序"><span class="toc-number">2.3.</span> <span class="toc-text">自然排序</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#尾部游标"><span class="toc-number">2.4.</span> <span class="toc-text">尾部游标</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#GridFS:存储文件"><span class="toc-number">3.</span> <span class="toc-text">GridFS:存储文件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#开始使用GridFS:mongofiles"><span class="toc-number">3.1.</span> <span class="toc-text">开始使用GridFS:mongofiles</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#内部原理"><span class="toc-number">3.2.</span> <span class="toc-text">内部原理</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#服务器端脚本"><span class="toc-number">4.</span> <span class="toc-text">服务器端脚本</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#db-eval"><span class="toc-number">5.</span> <span class="toc-text">db.eval</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#什么时候使用"><span class="toc-number">5.1.</span> <span class="toc-text">什么时候使用</span></a></li></ol></li></ol>
		
		</div>
		
        <h2 id="数据库命令">数据库命令</h2><h3 id="命令工作原理">命令工作原理</h3><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">db</span>.<span class="keyword">test</span>.<span class="keyword">drop</span>()</span><br></pre></td></tr></table></figure>
<p>实际运行的是drop命令:<br><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.<span class="function"><span class="title">runCommand</span><span class="params">(&#123;<span class="string">"drop"</span>:<span class="string">"test"</span>&#125;)</span></span></span><br></pre></td></tr></table></figure></p>
<a id="more"></a>
<p>MongoDB中的命令其实是作为一种特殊类型的查询来实现的。这些查询是针对$cmd集合来执行。runCmmand命令就是接受一个命令文档，来执行等价的查询操作。因此drop调用实际上是这样：<br><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.<span class="variable">$cmd</span>.<span class="function"><span class="title">findOne</span><span class="params">(&#123;<span class="string">"drop"</span>:<span class="string">"test"</span>&#125;)</span></span></span><br></pre></td></tr></table></figure></p>
<p>mongodb服务器得到查询$cmd集合的请求时，会启动一套特殊的逻辑来处理，而不是交给普通的查询代码来执行。</p>
<p>访问有些命令需要管理员权限，必须在admin数据库里运行。</p>
<h3 id="命令参考">命令参考</h3><p>MongoDB支持超过75个命令，我们可以在shell中运行：db.listCommands()，或者从驱动程序中运行等价的命令listCommands。</p>
<p>浏览器管理员接口：<code>http://localhost:28017/_commands</code></p>
<ul>
<li><p>buildInfo</p>
<p>  管理专用命令，返回MongoDB服务器的版本号和主机等<br>  <code>{&quot;buildinfo&quot;:1}</code></p>
</li>
<li><p>collStats</p>
<p>  返回指定集合的统计信息<br>  <code>{&quot;collStats&quot;:&quot;coll&quot;}</code></p>
</li>
<li><p>distinct</p>
<p>  返回指定集合中满足条件的文档的指定键的所有不同值<br>  <code>{&quot;distinct&quot;:&quot;coll&quot;,&quot;key&quot;:&quot;key&quot;,&quot;query&quot;:&quot;query&quot;}</code></p>
</li>
<li><p>drop</p>
<p>  删除集合的所有数据<br>  <code>{&quot;drop&quot;:&quot;coll&quot;}</code></p>
</li>
<li><p>dropDatabase</p>
<p>  删除当前数据库的所有数据<br>  <code>{&quot;dropDatabase&quot;:1}</code></p>
</li>
<li><p>dropIndexes</p>
<p>  删除集合里面名称为name的索引，如果名称为*，则代表删除全部。<br>  <code>{&quot;dropIndexes&quot;:&quot;coll&quot;,&quot;index&quot;:&quot;name&quot;}</code></p>
</li>
<li><p>findAndModify</p>
<p>  用法在第3章有记录</p>
</li>
<li><p>getLastError</p>
<p>  查看对本集合执行的最后一次操作的信息。在w台服务器复制集合的最后操作之前，这个命令会阻塞(超时的毫秒数)<br>  <code>{&quot;getLastError&quot;:1 [,&quot;w&quot;:w[,&quot;wtimeout&quot;: timeout]]}</code></p>
</li>
<li><p>isMaster</p>
<p>  检查本服务器是主服务器还是从服务器<br>  <code>{&quot;isMaster&quot;:1}</code></p>
</li>
<li><p>ListCommands</p>
<p>  返回所有可以在服务器上运行的命令及相关信息<br>  <code>{&quot;ListCommands&quot;:1}</code></p>
</li>
<li><p>listDatabases</p>
<p>  管理专用命令，列出服务器上所有的数据库<br>  <code>{&quot;listDatabases&quot;:1}</code></p>
</li>
<li><p>ping</p>
<p>  检查服务器连接是否正常。<br>  <code>{&quot;ping&quot;:1}</code></p>
</li>
<li><p>renameCollection</p>
<p>  将集合a重命名为b，其中a和b都必须是完整的集合命名空间(例如”foo.bar”表示foo数据库中的bar集合)<br>  <code>{&quot;renameCollection&quot;:a,&quot;to&quot;:b}</code></p>
</li>
<li><p>repairDatabase</p>
<p>  修复并压缩当前数据库<br>  <code>{&quot;repairDatabase&quot;:1}</code></p>
</li>
<li><p>serverStatus</p>
<p>  返回服务器的管理统计信息<br>  <code>{&quot;serverStatus&quot;:1}</code></p>
</li>
</ul>
<h2 id="固定集合">固定集合</h2><p>前面提到的所有集合，都是动态创建的。这种集合会随着数据的增多而自动扩容。MongoDB同时还支持另外一种集合—固定集合。这种集合的特征就是，需要提前创建，并且大小固定。对于大小固定，我们可以想象其就像一个环形队列，当集合空间用完后，再插入的元素就会覆盖最初始的头部的元素。</p>
<h3 id="属性">属性</h3><p>插入快速、按照插入顺序查询也快，自动淘汰最早的数据。</p>
<h3 id="创建固定集合">创建固定集合</h3><figure class="highlight openscad"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.createCollection<span class="params">(<span class="string">"my_Coll"</span>, &#123;<span class="string">"capped"</span> : <span class="literal">true</span>, <span class="string">"size"</span> : <span class="number">100000</span>, <span class="string">"max"</span> : <span class="number">100</span>&#125; )</span>;</span><br></pre></td></tr></table></figure>
<p>上面，我们创建了名称为my_Coll的固定集合，其固定大小为100000字节，最多放置文档数目为100个。当指定了数量上限时，必须同时指定size，淘汰机制只有在size没有达到上限时，以max来控制文档数量，当size达到上限时，以size为准。</p>
<p>可以使用命令convertToCapped，将一个普通集合转化为一个固定集合：<br><figure class="highlight openscad"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.runCommand<span class="params">(&#123;<span class="string">"convertToCapped"</span> : <span class="string">"test"</span>, <span class="string">"size"</span> : <span class="number">10000</span>&#125;)</span>;</span><br></pre></td></tr></table></figure></p>
<h3 id="自然排序">自然排序</h3><p>固定集合有种特殊的排序方法，是自然排序。自然排序就是文档在磁盘上的顺序。因为固定集合的文档总是按照插入顺序存储的，自然排序就与此相同。</p>
<p>{“$natural”:1}排序表示与默认的排序一样，{“$natural”:-1}插入顺序的倒叙，非固定集合用自然排序的意义不大。<br><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db<span class="class">.coll</span><span class="class">.find</span>().<span class="function"><span class="title">sort</span><span class="params">(&#123;<span class="string">"$natural"</span>:<span class="number">1</span>&#125;)</span></span></span><br></pre></td></tr></table></figure></p>
<h3 id="尾部游标">尾部游标</h3><p>尾部游标是一种特殊的持久游标，这类游标会不断的获取最新结果，是受linux中tail -f 命令的启发开发出来的。但是注意mongo shell不支持尾部游标，所以我们看一个php例子：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$cursor</span> = <span class="variable">$coll</span>-&gt;find()-&gt;tailable();</span><br><span class="line"><span class="keyword">while</span>(<span class="keyword">true</span>)&#123;</span><br><span class="line">    <span class="keyword">If</span>( !<span class="variable">$cursor</span>-&gt;hasNext() )&#123;</span><br><span class="line">        <span class="keyword">If</span>( <span class="variable">$cursor</span>-&gt;dead() )&#123;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        sleep(<span class="number">1</span>);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="keyword">while</span>( <span class="variable">$cursor</span>-&gt;hasNext() )&#123;</span><br><span class="line">            do_stuff(<span class="variable">$cursor</span>-&gt;getNext());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="GridFS:存储文件">GridFS:存储文件</h2><h3 id="开始使用GridFS:mongofiles">开始使用GridFS:mongofiles</h3><p>Mongofiles内置在MongoDB发布版中，可以用来在GridFS中上传，下载，列示，查找或删除文件。像执行其他命令行工具一样，执行mongofiles —help可以查看可用选项。</p>
<p>现在我们向GridFS上传文件，列出文件和下载文件。<br><figure class="highlight elixir"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$ </span>echo <span class="string">"hello,word"</span> &gt; foo.txt</span><br><span class="line"><span class="variable">$ </span>./mongofiles put foo.txt <span class="comment">#上传文件</span></span><br><span class="line"><span class="variable">$ </span>./mongofiles list <span class="comment">#列出文件</span></span><br><span class="line"><span class="variable">$ </span> rm foo.txt <span class="comment">#删除本地文件</span></span><br><span class="line"><span class="variable">$ </span>./mongofiles get foo.txt <span class="comment">#下载文件</span></span><br><span class="line"><span class="variable">$ </span>cat foo.txt</span><br><span class="line"><span class="variable">$ </span>./mongofiles search foo.txt <span class="comment">#查询文件</span></span><br><span class="line"><span class="variable">$ </span>./mongofiles delete foo.txt <span class="comment">#删除Grid上的文件</span></span><br></pre></td></tr></table></figure></p>
<h3 id="内部原理">内部原理</h3><p>GridFS的块有个单独的集合，默认情况下，都会使用fs.chunks集合，结构如下：</p>
<ul>
<li>_id #块的唯一键</li>
<li>n #块编号，也就是这个块在原文件中的编号</li>
<li>data #包含组成文件块的二进制数据</li>
<li>files_id #包含这个块原数据的文件文档 “_id”</li>
</ul>
<p>文件的原数据都放在另一个集合中，fs.files里面。结构如下：</p>
<ul>
<li>_id #文件的唯一id,对应files_id</li>
<li>length #文件内容的字节数</li>
<li>chunkSize #每块的大小，以字节为单位，默认是256K</li>
<li>updateDate #文件存入GridFS的时间戳</li>
<li>md5 #文件内容的md5校验和，由服务器端生成</li>
</ul>
<p>我们可以使用distinct命令获取GridFS中不重复的文件名列表。<br><figure class="highlight nimrod"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.fs.files.<span class="keyword">distinct</span>(<span class="string">"filename"</span>)</span><br></pre></td></tr></table></figure></p>
<h2 id="服务器端脚本">服务器端脚本</h2><h2 id="db-eval">db.eval</h2><p>使用db.eval函数可以在服务器端执行javascript脚本。这个函数先将给定的javascript字符串传给MongoDB，然后返回结果。</p>
<p>发送代码有两种选择，或者封装进一个函数，或者不封装。下面两行代码是等价的。</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">db.<span class="function"><span class="title">eval</span><span class="params">(<span class="string">"return 1;"</span>)</span></span></span><br><span class="line">db.<span class="function"><span class="title">eval</span><span class="params">(<span class="string">"function()&#123;return 1;&#125;"</span>)</span></span></span><br><span class="line">``` </span><br><span class="line"></span><br><span class="line">只有传递参数时需要封装成一个函数，参数通过db.eval的第二个参数传递，不过要写成一个数组的形式。例如传递给函数username。</span><br></pre></td></tr></table></figure>
<p>db.eval(“function(u){print(‘hello’+ u +’!’);}”,[username])<br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">有必要的话，可以传递多个参数，例如要计算3个数之和。</span><br></pre></td></tr></table></figure></p>
<p>db.eval(“function(x,y,z){return x+y+z;}”,[num1,num2,num3])<br><figure class="highlight perl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"> </span><br><span class="line"><span class="comment">### 存储Javascript</span></span><br><span class="line"></span><br><span class="line">每个MongoDB的数据库中都有个特殊的集合，叫做<span class="keyword">system</span>.js，用来存放Javascript变量。这些变量可以在任何MongoDB的javascript上下文中调用，包括<span class="variable">$where</span>字句，db.<span class="keyword">eval</span>调用，MapReduce作业。用insert可以将变量加入到<span class="keyword">system</span>.js之中。</span><br></pre></td></tr></table></figure></p>
<p>db.system.js.insert({“_id”:”x”,”value”:1})<br><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"> </span><br><span class="line"><span class="tag">System</span><span class="class">.js</span>还可以存入<span class="tag">Javascript</span>代码。如用<span class="tag">javascript</span>写一个日志函数，将其放在<span class="tag">system</span><span class="class">.js</span>中。</span><br></pre></td></tr></table></figure></p>
<p>db.system.js.insert({“_id”:”log”,”value”:function(msg,level){<br>var levels = [‘DEBUG’,’WARNING’,’ERROR’,’FATAL’];<br>level = level ? level : 0 ;<br>var now = new Date();<br>print(now + “ “ + levels[level] + msg );<br>}})<br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">现在我们可以调用这个函数：</span><br></pre></td></tr></table></figure></p>
<p>db.eval(“x=’hello’;log(‘x is’+x,1);”);<br><figure class="highlight vala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"> </span><br><span class="line"><span class="preprocessor">### 安全性</span></span><br></pre></td></tr></table></figure></p>
<p>func = “function(){print(‘Hello,”+username+”!’);}”<br><figure class="highlight coffeescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">上面代码可以实现打印欢迎信息，但如果username是`<span class="javascript"><span class="string">"');db.dropDatabase();print('"</span></span>`,上面代码就变成了</span><br></pre></td></tr></table></figure></p>
<p>func = “function(){print(‘Hello,’);db.dropDatabase();print(‘!’);}”<br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">数据库被清空了</span><br><span class="line"></span><br><span class="line">为了避免这种情况，要限定作用域，PHP中代码如下：</span><br></pre></td></tr></table></figure></p>
<p>$func=new MongoCode(“function(){<br>print(‘hello,”+username+”!’);<br>}”,array(“username” =&gt; $username))<br><figure class="highlight vala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="preprocessor">## 数据库引用</span></span><br><span class="line"></span><br><span class="line">MongoDB最鲜为人知的功能就是数据库引用，也叫DBRef。DBRef就像Url，唯一确定一个到文档的引用。它加载的方式如同浏览器加载url一样。</span><br><span class="line"></span><br><span class="line">DBRef是个内嵌文档，下面有几个是DBRef的必选键,注意必须建的顺序不能改变。</span><br></pre></td></tr></table></figure></p>
<p>{“$ref”:collection,”$id”:id_value}<br>$ref #指定集合<br>$id #指定文档的 _id<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"> </span><br><span class="line">如果想用另外一个数据的文档，还有个<span class="variable">$db</span>键，这个键是可选的</span><br></pre></td></tr></table></figure></p>
<p>{“$ref”:collection,”$id”:id_value,”$db”:database}<br><figure class="highlight vala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="preprocessor">### 示例模式</span></span><br><span class="line"></span><br><span class="line">users集合：</span><br></pre></td></tr></table></figure></p>
<p>{“_id”,”mike”,”display_name”:”Mike D”}<br>{“_id”,”kristina”,”display_name”:”Kristina C”}<br><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">notes</span>集合：</span><br></pre></td></tr></table></figure></p>
<p>{“_id”:5,”author”:”mike”,”text”:”mongodb is fun”}<br>{“_id”:25,”author”:”kristina”,”text”:” DBREF “, “references”[{“$ref”:”users”,”$id”:”mike”},{“$ref”:”notes”,”$id”:5}]}<br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">第二个笔记包含一些对其它文档的引用，每一条都作为一个DBRef存储。</span><br></pre></td></tr></table></figure></p>
<p>var note = db.notes.findOne({“_id”:20})<br>note.references.forEach(function(ref){<br>    printjson(db[ref.$ref].findOne({“_id”:ref.$id}))<br>});<br><figure class="highlight vala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="preprocessor">### 驱动对DBRef的支持</span></span><br><span class="line"></span><br><span class="line">下面是使用PyMongo中的DBRef类型。</span><br></pre></td></tr></table></figure></p>
<p>note = {“_id”:25,”author”:”kristina”,”text”:” DBREF “, “references”:[DBRef(“users”,”mike”),DBRef(“notes”,5)]}<br>```</p>
<h3 id="什么时候使用">什么时候使用</h3><p>存储一些对不同集合的文档的引用时，最好使用DBRef，就像前面的例子。或者想使用驱动程序或者工具中DBRef特有的功能，只能使用DBRef了。否则，最好存储”_id”作为引用来使用，因为这样更精简，也更容易操作。</p>

      
    </div>
    <footer class="article-footer">
	  
	  <!-- 百度分享 Start -->
	  <div class="bdsharebuttonbox"><a href="#" class="bds_more" data-cmd="more"></a><a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a><a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a><a href="#" class="bds_tqq" data-cmd="tqq" title="分享到腾讯微博"></a><a href="#" class="bds_renren" data-cmd="renren" title="分享到人人网"></a><a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a></div>
	  <!-- 百度分享 End -->
	  
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/MongoDB/">MongoDB</a></li></ul>

	  
<span>
Updated:<time datetime="2014-12-24T07:55:07.000Z" itemprop="dateModified">2014-12-24</time>
</span>


    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2014/06/05/mongodb-note-8-administration/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          mongodb笔记(八) 管理
        
      </div>
    </a>
  
  
    <a href="/2014/05/28/mongodb-note-6-aggregation/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">mongodb笔记(六) 聚合</div>
    </a>
  
</nav>

  
</article>



<!-- 多说评论框 start -->

<section id="comments">
  <div class="ds-thread"  data-thread-key="/2014/06/04/mongodb-note-7-advanced-topics/" data-title="mongodb笔记(七) 进阶指南" data-url="http://howiefh.github.io/2014/06/04/mongodb-note-7-advanced-topics/" id="ds_thread">
    <noscript>Please enable JavaScript to view the <a href="//duoshuo.com/?ref_noscript">comments powered by duoshuo.</a></noscript>
  </div>
</section>

<!-- 多说评论框 end -->
</section>
        
          
  <div id="toc" class="toc-aside">
  <h2 class="toc-title">Contents</h2>
    
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#数据库命令"><span class="toc-number">1.</span> <span class="toc-text">数据库命令</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#命令工作原理"><span class="toc-number">1.1.</span> <span class="toc-text">命令工作原理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#命令参考"><span class="toc-number">1.2.</span> <span class="toc-text">命令参考</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#固定集合"><span class="toc-number">2.</span> <span class="toc-text">固定集合</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#属性"><span class="toc-number">2.1.</span> <span class="toc-text">属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建固定集合"><span class="toc-number">2.2.</span> <span class="toc-text">创建固定集合</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#自然排序"><span class="toc-number">2.3.</span> <span class="toc-text">自然排序</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#尾部游标"><span class="toc-number">2.4.</span> <span class="toc-text">尾部游标</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#GridFS:存储文件"><span class="toc-number">3.</span> <span class="toc-text">GridFS:存储文件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#开始使用GridFS:mongofiles"><span class="toc-number">3.1.</span> <span class="toc-text">开始使用GridFS:mongofiles</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#内部原理"><span class="toc-number">3.2.</span> <span class="toc-text">内部原理</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#服务器端脚本"><span class="toc-number">4.</span> <span class="toc-text">服务器端脚本</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#db-eval"><span class="toc-number">5.</span> <span class="toc-text">db.eval</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#什么时候使用"><span class="toc-number">5.1.</span> <span class="toc-text">什么时候使用</span></a></li></ol></li></ol>
    
  </div>

<aside id="sidebar">

  
    
<div class="widget-wrap">
  <h3 class="widget-title">ABOUT ME</h3>
  <ul class="widget about-me">
    
    <li><img class="author" title="About me" src="http://fh-1.qiniudn.com/okal-eltocat.jpg" /></li>
    
    
    <li>Hi,I'm FengHao.</li>
    
    <li>I'll share something interesting and my learning experience with you at this blog.</li>
    
    <li>前博客:<a href="http://hi.baidu.com/idea_star" target="_BLANK">百度空间</a></li>
    
  </ul>
</div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Android/">Android</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/C/">C</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Database/">Database</a><span class="category-list-count">13</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/Database/MongoDB/">MongoDB</a><span class="category-list-count">10</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Database/MySQL/">MySQL</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/Eclipse/">Eclipse</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/FTP/">FTP</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Git/">Git</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Hexo/">Hexo</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java/">Java</a><span class="category-list-count">20</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/Java/FreeMarker/">FreeMarker</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java/Shiro/">Shiro</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaEE/">JavaEE</a><span class="category-list-count">4</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/JavaEE/Hibernate/">Hibernate</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaEE/JSP/">JSP</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaEE/Spring/">Spring</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaScript/">JavaScript</a><span class="category-list-count">5</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/JavaScript/jQuery/">jQuery</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/Linux/">Linux</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/PHP/">PHP</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Suse/">Suse</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Ubuntu/">Ubuntu</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Vim/">Vim</a><span class="category-list-count">8</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/编程/">编程</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/软件/">软件</a><span class="category-list-count">1</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Android/" style="font-size: 14.75px; color: #65bfa7">Android</a> <a href="/tags/C/" style="font-size: 13.88px; color: #71c1c2">C</a> <a href="/tags/CAS/" style="font-size: 13px; color: #7dc3de">CAS</a> <a href="/tags/Chrome/" style="font-size: 13px; color: #7dc3de">Chrome</a> <a href="/tags/Clean-Code/" style="font-size: 13.88px; color: #71c1c2">Clean Code</a> <a href="/tags/Database/" style="font-size: 13px; color: #7dc3de">Database</a> <a href="/tags/Eclipse/" style="font-size: 13px; color: #7dc3de">Eclipse</a> <a href="/tags/FTP/" style="font-size: 13.88px; color: #71c1c2">FTP</a> <a href="/tags/FreeMarker/" style="font-size: 14.75px; color: #65bfa7">FreeMarker</a> <a href="/tags/Gcc/" style="font-size: 13px; color: #7dc3de">Gcc</a> <a href="/tags/Git/" style="font-size: 14.75px; color: #65bfa7">Git</a> <a href="/tags/Github-Pages/" style="font-size: 13px; color: #7dc3de">Github Pages</a> <a href="/tags/Hexo/" style="font-size: 14.75px; color: #65bfa7">Hexo</a> <a href="/tags/Hibernate/" style="font-size: 13px; color: #7dc3de">Hibernate</a> <a href="/tags/JSP/" style="font-size: 13px; color: #7dc3de">JSP</a> <a href="/tags/JVM/" style="font-size: 14.75px; color: #65bfa7">JVM</a> <a href="/tags/Java/" style="font-size: 14.75px; color: #65bfa7">Java</a> <a href="/tags/JavaMail/" style="font-size: 13px; color: #7dc3de">JavaMail</a> <a href="/tags/JavaScript/" style="font-size: 16.5px; color: #4dbc6f">JavaScript</a> <a href="/tags/Linux/" style="font-size: 13.88px; color: #71c1c2">Linux</a> <a href="/tags/Log/" style="font-size: 13px; color: #7dc3de">Log</a> <a href="/tags/Markdown/" style="font-size: 13.88px; color: #71c1c2">Markdown</a> <a href="/tags/MongoDB/" style="font-size: 20px; color: #1db400">MongoDB</a> <a href="/tags/MySQL/" style="font-size: 13.88px; color: #71c1c2">MySQL</a> <a href="/tags/PHP/" style="font-size: 16.5px; color: #4dbc6f">PHP</a> <a href="/tags/Rhythmbox/" style="font-size: 13px; color: #7dc3de">Rhythmbox</a> <a href="/tags/SSO/" style="font-size: 13px; color: #7dc3de">SSO</a> <a href="/tags/Servlet/" style="font-size: 13px; color: #7dc3de">Servlet</a> <a href="/tags/Shiro/" style="font-size: 13.88px; color: #71c1c2">Shiro</a> <a href="/tags/Spring/" style="font-size: 13.88px; color: #71c1c2">Spring</a> <a href="/tags/Suse/" style="font-size: 13px; color: #7dc3de">Suse</a> <a href="/tags/Thinking-in-Java/" style="font-size: 19.13px; color: #29b61c">Thinking in Java</a> <a href="/tags/Ubuntu/" style="font-size: 17.38px; color: #41ba53">Ubuntu</a> <a href="/tags/Vim/" style="font-size: 18.25px; color: #35b838">Vim</a> <a href="/tags/VirtualBox/" style="font-size: 13px; color: #7dc3de">VirtualBox</a> <a href="/tags/Vsftpd/" style="font-size: 13px; color: #7dc3de">Vsftpd</a> <a href="/tags/jQuery/" style="font-size: 13px; color: #7dc3de">jQuery</a> <a href="/tags/pam-mysql/" style="font-size: 13px; color: #7dc3de">pam_mysql</a> <a href="/tags/小米/" style="font-size: 13px; color: #7dc3de">小米</a> <a href="/tags/软件/" style="font-size: 15.63px; color: #59bd8b">软件</a>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/09/">September 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/08/">August 2015</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/07/">July 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/05/">May 2015</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/04/">April 2015</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/03/">March 2015</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/02/">February 2015</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/12/">December 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/11/">November 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/10/">October 2014</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/09/">September 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/08/">August 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/07/">July 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/06/">June 2014</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/05/">May 2014</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/04/">April 2014</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/03/">March 2014</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/02/">February 2014</a><span class="archive-list-count">11</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/01/">January 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/12/">December 2013</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/11/">November 2013</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/08/">August 2013</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/07/">July 2013</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/06/">June 2013</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/05/">May 2013</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/04/">April 2013</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/03/">March 2012</a><span class="archive-list-count">1</span></li></ul>
    </div>
  </div>


  
    <div class="widget-wrap">
  <h3 class="widget-title">Calendar</h3>
  <div class="widget">
    <div id="g-calendar" class="g-calendar">
        <span class="g-calendar-prev"></span>
		 
        <span class="g-calendar-back"></span>
        <span class="g-calendar-now"></span>
		 
        <span class="g-calendar-next"></span>
        <div class="g-calendar-hd"></div>
        <div class="g-calendar-tit"></div>
        <div class="g-calendar-bd"></div>
    </div>
  </div>
</div>
<script type="text/javascript">
function LGY_calendar(wrapId, options){
    this.oWrap = this.getId(wrapId);
    this.oHead = this.getByClassName('g-calendar-hd',this.oWrap)[0];
    this.oBody = this.getByClassName('g-calendar-bd',this.oWrap)[0];
    this.oTit = this.getByClassName('g-calendar-tit',this.oWrap)[0];
    this.oPrev = this.getByClassName('g-calendar-prev',this.oWrap)[0];
    this.oNext = this.getByClassName('g-calendar-next',this.oWrap)[0];
    this.oNow = this.getByClassName('g-calendar-now',this.oWrap)[0];
    this.oBack = this.getByClassName('g-calendar-back',this.oWrap)[0];
    this.init();
}
LGY_calendar.prototype = {
    ///////////获取ID元素
    getId:function(id){
        return document.getElementById(id);
    },
    ////////获取css类名元素
    getByClassName:function(className,parent){
        var elem = [],
            node = parent != undefined&&parent.nodeType==1?parent.getElementsByTagName('*'):document.getElementsByTagName('*'),
            p = new RegExp("(^|\\s)"+className+"(\\s|$)");
        for(var n=0,i=node.length;n<i;n++){
            if(p.test(node[n].className)){
                elem.push(node[n]);
            }
        }
        return elem;
    },
    //填充日历
    fillDate:function(year,month){
        //本月份第一天是星期几-为显示上个月的天数做铺垫
        var first_day = new Date(year,month,1).getDay(),
        //如果刚好是星期天，则空出一行（显示上个月的天数）
            first_day = first_day == 0?first_day=7:first_day;
        //本月份最后一天是几号
        var final_date = new Date(year,month+1,0).getDate(),
        //上个月的最后一天是几号
            last_date = new Date(year,month,0).getDate(),
        //剩余的格子数--即排在末尾的格子数
            surplus = 42 - first_day - final_date;
        //设置年的链接
        var yearHead = "<a href='/" + "archives/" + year + "/'>" + year + " "+ "</a>"; 
        //设置年的链接
        var monthHead = "";
        var month1 = month + 1;
        if (month1 < 10) {
            monthHead = "<a href='/" + "archives/" + year + "/" + "0" + month1 + "/'>" + " " + month1 + " " + "</a>";
        } else {
            monthHead = "<a href='/" + "archives/" + year + "/" + month1 + "/'>" + " " + month1 + " " + "</a>";
        }
        //设置表头的日历
        this.oHead.innerHTML = yearHead+'年'+monthHead+'月';
        //填充日历执行
        var html = '';
        //上个月的显示天数
        for(var i=0;i<first_day;i++){
            html+='<span class="g-calendar-grey">'+(last_date-(first_day-1)+i)+'</span>';
        }
        //本月的显示天数
        var postdate = new Date("Wed Jun 04 2014 09:43:00 GMT+0800"); 
        if (true && postdate.getFullYear() == year && postdate.getMonth() == month) { 
            html += '<span>1</span><span>2</span><span>3</span><span><a href="/2014/06/04/mongodb-note-7-advanced-topics/" title="mongodb笔记(七) 进阶指南">4</a></span><span><a href="/2014/06/05/mongodb-note-9-replication/" title="当天有2篇博客,第一篇:mongodb笔记(九) 复制">5</a></span><span><a href="/2014/06/06/mongodb-note-10-sharding/" title="mongodb笔记(十) 分片">6</a></span><span>7</span><span>8</span><span>9</span><span><a href="/2014/06/10/clean-code-note-1/" title="代码整洁之道(一)">10</a></span><span>11</span><span>12</span><span>13</span><span>14</span><span><a href="/2014/06/15/clean-code-note-2/" title="代码整洁之道(二)">15</a></span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span><span>25</span><span>26</span><span>27</span><span>28</span><span>29</span><span>30</span><span>31</span>';
        } else {
            for(var j=0;j<final_date;j++){        
                html+='<span id="d'+(j+1)+'">'+(j+1)+'</span>';
            }
        }
        //下个月的显示天数
        for(var k=0;k<surplus;k++){
            html+='<span class="g-calendar-grey">'+(k+1)+'</span>';
        }
        //fill
        this.oBody.innerHTML = html;
        // 当前状态
        if(year==this.c_year&&this.c_month==month){
            this.oBody.getElementsByTagName('span')[first_day+this.date-1].className='g-calendar-on';
        }

        // 对所有文章遍历,然后将有文章的日期加上链接,如果文章太多的话,生成页面会很大,去掉了
        
    },
    // next切换
    next:function(){
        var _that = this;
        this.oNext.onclick = function(){
            _that.month++;
            if(_that.month>11){
                _that.month = 0;
                _that.year++;
            }
            // 填充日历
            _that.fillDate(_that.year,_that.month);
        };
    },
    // back 切换
    back:function(){
        var _that = this;
        if(this.oBack != undefined) {
            this.oBack.onclick = function(){
                var postdate = new Date("Wed Jun 04 2014 09:43:00 GMT+0800"); 
                _that.year = postdate.getFullYear();
                _that.month = postdate.getMonth();
                // 填充日历
                _that.fillDate(_that.year,_that.month);
            };
        }
    },
    // now切换
    now:function(){
        var _that = this;
        if(this.oNow != undefined ) {
            this.oNow.onclick = function(){
                var nowDate = new Date(); 
                _that.year = nowDate.getFullYear();
                _that.month = nowDate.getMonth();
                // 填充日历
                _that.fillDate(_that.year,_that.month);
            };
        }
    },
    // prev切换
    prev:function(){
        var _that = this;
        this.oPrev.onclick = function(){
            _that.month--;
            if(_that.month<0){
                _that.month = 11;
                _that.year--;
            }
            // 填充日历
            _that.fillDate(_that.year,_that.month);
        };
    },
    init:function(){
        this.oTit.innerHTML = '<span>日</span><span>一</span><span>二</span><span>三</span><span>四</span><span>五</span><span>六</span>';
        // 获取今天的日历时间
        var now = new Date();
        this.c_year = this.year = now.getFullYear();
        this.c_month = this.month = now.getMonth();
        this.date = now.getDate();
        // 初始化--填充日历
        this.fillDate(this.year,this.month);
        //next切换
        this.next();
        //prev切换
        this.prev();
        //back 切换
        this.back();
        //now 切换
        this.now();
    }
}
new LGY_calendar('g-calendar');
</script>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2015/09/01/javascript-summary/">JavaScript 总结</a>
          </li>
        
          <li>
            <a href="/2015/08/28/javascript-oop-function-expression-and-async/">JavaScript 面向对象程序设计、函数表达式和异步编程</a>
          </li>
        
          <li>
            <a href="/2015/08/28/javascript-reference-type/">JavaScript 引用类型</a>
          </li>
        
          <li>
            <a href="/2015/08/28/javascript-grammar/">JavaScript 基本语法</a>
          </li>
        
          <li>
            <a href="/2015/08/10/java-web/">Java Web 笔记</a>
          </li>
        
      </ul>
    </div>
  </div>


  
    
<div class="widget-wrap">
  <h3 class="widget-title">Recent Comments</h3>
  <ul class="widget ds-recent-comments" data-num-items="5" data-show-avatars="0" data-show-title="1" data-show-time="1"></ul>
</div>
<!-- 需要多说的公用代码 -->


  

</aside>

        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2015 howiefh<br>
      Powered by <a href="http://zespia.tw/hexo/" target="_blank">Hexo</a> and Theme by <a href="https://github.com/howiefh/hexo-theme-landscape-f" target="_blank" title="Landscape-F">Landscape-F</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<!-- 多说公共JS代码 start (一个网页只需插入一次) -->

<script type="text/javascript">
  var duoshuoQuery = {short_name:"howiefh"};
  (function() {
	var ds = document.createElement('script');
	ds.type = 'text/javascript';ds.async = true;
	ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
	ds.charset = 'UTF-8';
	(document.getElementsByTagName('head')[0] 
		|| document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
</script> 

<!-- 多说公共JS代码 end -->

<!-- 百度分享 start -->

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":["mshare","douban","bdysc","sqq","qq","hi","baidu","huaban","youdao","sdo","mail","xg","diandian","fx","copy","print"],"bdPic":"","bdStyle":"1","bdSize":"16"},"share":{},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到：","viewSize":"16"}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>

<!-- 百度分享 end -->

<!--
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="/js/jquery.min.js"></script>
-->
<script src="/js/jquery.min.js" type="text/javascript"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
  <script src="/fancybox/jquery.fancybox.pack.js" type="text/javascript"></script>


<div class="bottom-btn">

	<a class="icon-gotop" href="javascript:void(0)" title="返回顶部"></a>
	<script src="/js/gotop.js" type="text/javascript"></script>
	<!--
	<script src="/js/gotop.js"></script>
	-->


	<a class="icon-toc-toggle" href="javascript:void(0)" title="文章目录"></a>
	<!--
	<script src="/js/toc_aside_toggle.js"></script>
	-->

</div>
<script src="/js/toc_aside_toggle.js" type="text/javascript"></script>


<script src="/js/script.js" type="text/javascript"></script>

  </div>
</body>
</html>
